<?php
/*  	shipping_id
		invoice_number
		cost
*/
function reequipment_user_shipping()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:update', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('item_id', 'int:1:', $item_id, '', XARVAR_NOT_REQUIRED)) return;  
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('shipping_id', 'int:1:', $shipping_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('invoice_number', 'str:1:', $invoice_number, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('cost', 'float', $cost, '', XARVAR_NOT_REQUIRED)) return;       
    
    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();
    xarTplSetPageTitle(xarML('Shipping Cost'));
    // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return; 
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');
    
    // Check to make sure this uid is allowed for this company
    $uid = xarSessionGetVar('uid');
    // Need to check for multiple company ids first.  If there are, then we need to set the one that we are working with.
    $count = xarModAPIFunc('reequipment', 'user', 'countcompaniesforuser', array('user_id' => $uid));  
    if ((empty($company_id) AND ($count > 1))){
		$data['set_company'] = true;
		// return var_export($data['set_company']);
		$data['companies'] = xarModAPIFunc('reequipment', 'user', 'getrel', array('user_id' => $uid));
		$data['change_companies'] = true;
		return $data;
	}
	// Also, give a chance to change companies if you are assigned to multiple companies.
	if ($count > 1){
		$data['change_companies'] = true;
	} else {
		$data['change_companies'] = false;
	}
	// Override the company id if you are just a part of one company.
	if ($count == 1){
		// Get Company ID.
		$company = xarModAPIFunc('reequipment', 'user', 'getrel', array('user_id' => $uid));
		// return var_export($company);
		foreach ($company as $var){
			// make sure it is an integer.
			$company_id = intval($var['company_id']);
		}
	}    
	
    switch(strtolower($phase)) {
        case 'form':
            
            // Last 10 invoices
            $items = xarModAPIFunc('reequipment', 'user', 'getitems', array('item_id' => $item_id));
            foreach($items as $var){
            	$data['form_invoice_number'] = $var['invoice_number'];
            	$data['form_company_id'] = intval($var['company_id']);
            }
            // Ensure someone isn't playing with the URL 
            $uid = xarSessionGetVar('uid');
            $company = xarModAPIFunc('reequipment', 'user', 'getrel', array('user_id' => $uid));
			foreach ($company as $var){
				// make sure it is an integer.
				$company_id = intval($var['company_id']);
			}
			if ($company_id != $data['form_company_id']){
				$data['invalid_company'] = true;
			}
			$data['invalid_company'] = false;
            // return var_export($data['schedules']);
            $data['form_company_id'] = $company_id;
            $cost = xarModAPIFunc('reequipment', 'user', 'getshipping', array('invoice_number' => $data['form_invoice_number']));
            foreach ($cost as $var){
            	$data['form_cost'] = $var['cost'];
            	$data['form_shipping_id'] = $var['shipping_id'];
            	$data['form_phase'] = 'update';
            	$data['form_label'] = xarML('Edit Shipping Cost');
            }
            if (!isset($data['form_cost'])){
            	$data['form_cost'] = '0.00';
            	$data['form_shipping_id'] = '';
            	$data['form_phase'] = 'create';
            	$data['form_label'] = xarML('Add Shipping Cost');
            }
            	
            	
        break;

        case 'create':
        		if (empty($invoice_number)) {
        			xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        		}
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createshipping', array('invoice_number' => $invoice_number, 'cost' => $cost, 'company_id' => $company_id));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        break; 
        
        case 'update':
        		if (empty($invoice_number)) {
        			xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        		}
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateshipping', array('shipping_id' => $shipping_id, 'invoice_number' => $invoice_number, 'cost' => $cost, 'company_id' => $company_id));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        break; 
    }
    // Return the output
    return $data;
}
?>